package com.heima.behavior.service.Imp;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApCollectionBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.CollectionBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.common.constants.article.HotArticleConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.user.pojo.ApUser;
import com.heima.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class ApCollectionBehaviorServiceImp implements ApCollectionBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult collectBehavior(CollectionBehaviorDTO dto) {
        ApBehaviorEntry behaviorEntry = creatBehaviorEntryMsg(dto);
        //判断是否登录 登录才能收藏
        if (behaviorEntry.getType() != 1) {
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN,"未登录请先登录");
            log.info("未登录请先登录");
        }
        //查询收藏是否存在
        Query query = Query.query(Criteria.where("entryId").is(behaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);

        if (apCollection == null && dto.getOperation() == 0) {
            apCollection = new ApCollection();
            apCollection.setEntryId(behaviorEntry.getId());
            apCollection.setArticleId(dto.getArticleId());
            apCollection.setCreatedTime(new Date());
            mongoTemplate.save(apCollection);
        } else {
            //如果存在 取消就删除
            mongoTemplate.remove(query,ApCollection.class);
        }
        //===================================
        NewBehaviorDTO mess = new NewBehaviorDTO();
        mess.setArticleId(dto.getArticleId());
        mess.setType(NewBehaviorDTO.BehaviorType.COLLECTION);
        mess.setAdd(dto.getOperation() == 0?1:-1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(mess));
        log.info("收藏行为 发送消息=={}",JSON.toJSONString(mess));
        //===================================
        return ResponseResult.okResult();
    }

    /**
     * 获取行为创建
     * @param dto
     * @return
     */
    private ApBehaviorEntry creatBehaviorEntryMsg(CollectionBehaviorDTO dto) {
        //获得用户token中的id检查是否处于登陆状态
        ApUser user = AppThreadLocalUtils.getUser();
        //对数据进行判空
        if (user == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"用户未携带ID或未携带设备ID");
        }
        //调用
        // return  ApBehaviorEntryService(user.getId(),likesBehaviorDTO.getEquipmentId());
        return apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(),dto.getEquipmentId());
    }
}
